using System.Collections.Generic;
using System.Linq;
using UnityEngine;

namespace Demo.Linq
{
    struct A_Struct
    {
        public int i;
        public double d;
        public float f;
    }

    public class LinqExample : MonoBehaviour
    {
        List<A_Struct> alist = new List<A_Struct>();

        void Start()
        {
            Debug.Log("=======linq对结构体内int大小排序======");
            alist.Clear();
            alist = new List<A_Struct>();
            alist.Add(new A_Struct() { i = 17 });
            alist.Add(new A_Struct() { i = 3 });
            alist.Add(new A_Struct() { i = 20 });
            alist.Add(new A_Struct() { i = 13 });
            alist.Add(new A_Struct() { i = 2 });
            alist.Add(new A_Struct() { i = 10 });
            alist.Add(new A_Struct() { i = 9 });
            alist = alist.OrderBy(v => v.i).ToList();
            for (int i = 0; i < alist.Count; i++)
            {
                Debug.Log($"Index{i}:  {alist[i].i}");
            }
            

            Debug.Log("=======linq对结构体内double大小排序======");
            alist.Clear();
            alist = new List<A_Struct>();
            alist.Add(new A_Struct() { d = 17.5667d, f = 17.5667f });
            alist.Add(new A_Struct() { d = 3.4890d, f = 3.4890f });
            alist.Add(new A_Struct() { d = 20.5001d, f = 20.5001f });
            alist.Add(new A_Struct() { d = 13.2491d, f = 13.2491f });
            alist.Add(new A_Struct() { d = 2.3401d, f = 2.3401f });
            alist.Add(new A_Struct() { d = 10.0001d, f = 10.0001f });
            alist.Add(new A_Struct() { d = 9.8275d, f = 9.8275f });
            alist = alist.OrderBy(v => v.d).ToList();
            for (int i = 0; i < alist.Count; i++)
            {
                Debug.Log($"Index{i}:  {alist[i].d}");
            }


            Debug.Log("=======linq对结构体内float大小排序======");
            alist.Clear();
            alist = new List<A_Struct>();
            alist.Add(new A_Struct() { d = 17.5667d, f = 17.5667f });
            alist.Add(new A_Struct() { d = 3.4890d, f = 3.4890f });
            alist.Add(new A_Struct() { d = 20.5001d, f = 20.5001f });
            alist.Add(new A_Struct() { d = 13.2491d, f = 13.2491f });
            alist.Add(new A_Struct() { d = 2.3401d, f = 2.3401f });
            alist.Add(new A_Struct() { d = 10.0001d, f = 10.0001f });
            alist.Add(new A_Struct() { d = 9.8275d, f = 9.8275f });
            alist = alist.OrderBy(v => v.f).ToList();
            for (int i = 0; i < alist.Count; i++)
            {
                Debug.Log($"Index{i}:  {alist[i].f}");
            }
        }
    }
}